package com.example.zcl.web;

import com.example.zcl.system.entity.User;
import com.example.zcl.system.result.Result;
import com.example.zcl.system.result.ResultUtil;
import com.example.zcl.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: --
 * @author: Bing
 * @date: 2025-05-29 15:18
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public Result getAllUsers() {
        List<User> list = userService.list();
        return ResultUtil.success(list);
    }

    @PostMapping
    public Result createUser(@RequestBody User user) {
        userService.save(user);
        return ResultUtil.success();
    }

    @GetMapping("/{username}")
    public Result getUserByUsername(@PathVariable String username) {
        userService.getByUsername(username);
        return ResultUtil.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return ResultUtil.success();
    }

    @PutMapping("/{id}")
    public Result updateUser(@PathVariable Long id, @RequestBody User user) {
        user.setId(id);
        userService.updateById(user);
        return ResultUtil.success();
    }
}
